package com.home.biz;

import com.github.pagehelper.PageHelper;
import com.home.base.biz.BaseBiz;
import com.home.base.exceptions.ServiceException;
import com.home.config.InitDataConfig;
import com.home.entity.FetchPhone;
import com.home.entity.LtPhone;
import com.home.entity.ProvInfo;
import com.home.mapper.FetchPhoneMapper;
import com.home.mapper.LtPhoneMapper;
import com.home.task.FetchTask;
import com.home.task.LtFetchTask;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author ：chenxf
 * @date ：Created in 2020/6/10 13:27
 * @description：
 * @modified By：
 * @version: $
 */
@Service
public class LtPhoneBiz extends BaseBiz<LtPhoneMapper, LtPhone> {

    @Autowired
    ProvBiz provBiz;
    @Autowired
    LtFetchTask fetchTask;

    /**

     * @param limit  条数
     * @param offset  页码
     * @return
     */
    public List<LtPhone> list(int limit, int offset, String phoneNum, String cityName){
        PageHelper.startPage(offset, limit);
        Example example = new Example(FetchPhone.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(phoneNum)){
            criteria.andLike("phoneNum","%"+phoneNum+"%");
        }
        if(StringUtils.isNotBlank(cityName)){
            criteria.andLike("cityName",cityName+"%");
        }
        example.setOrderByClause(" update_time desc");
        return selectByExample(example);

    }

    public void fetchOne(String sNum, String eNum,String tNum,String provName,String cityName){
        //线程开始采集
        deletePhone();
        if(InitDataConfig.isFetchStatus()){
            throw new ServiceException("有其它采集任务正在执行，请等待!");
        }else {
            fetchTask.activityTask(this,"联通商城号码",sNum,eNum,tNum);
        }

        //结束
    }



    public void deletePhone(){
        mapper.deletePhone();
    }

    public Integer counts(){
        return mapper.phoneCounts();
    }

    public void saveList(List<LtPhone> phone) {
        if(phone.size()==0){
            return;
        }
        phone.stream().forEach(e->save(e));

    }

    public void save(LtPhone fetchPhone) {

        try {
            insertSelective(fetchPhone);
        } catch (Exception e) {
            //e.printStackTrace();
        }

    }
}
